//
//  RHPackItemCell.m
//  RuiHeAppApp
//
//  Created by Yan Chen on 2023/12/6.
//

#import "RHPackItemCell.h"
@interface RHPackItemCell()

@property (nonatomic ,strong) UIImageView *batIcon;

@property (nonatomic ,strong) QMUILabel *volLab;

@property (nonatomic ,strong) CAShapeLayer *lineLayer;
@end
@implementation RHPackItemCell
-(instancetype)initWithFrame:(CGRect)frame
{
    if(self = [super initWithFrame:frame]){
        [self setupUI];
    }
    return self;
}

-(void)setCellModel:(RHDevCellModel *)cellModel
{
    _cellModel = cellModel;
    _cellModel.status = 1;
    if([cellModel.temp floatValue] > 55.0){
        _cellModel.status = 2;
    }
    if ([cellModel.temp floatValue] < -20.0) {
        _cellModel.status = 3;
    }
    if(_cellModel.status == 1){
        self.batIcon.image = [UIImage imageNamed:@"local_cell_temp_normal"];
    }else if(_cellModel.status == 2){
        self.batIcon.image = [UIImage imageNamed:@"local_cell_temp_high"];
    }else if(_cellModel.status == 3){
        self.batIcon.image = [UIImage imageNamed:@"local_cell_temp_low"];
    }
//    self.tempLab.text = [NSString stringWithFormat:@"%@℃",cellModel.temp];
    self.volLab.text = [NSString stringWithFormat:@"%@V",cellModel.volt];
    CGFloat tempWidth = [self.tempLab.text getStringWidthFont:self.tempLab.font limitHeight:20];
    CGFloat volWidth = [self.volLab.text getStringWidthFont:self.volLab.font limitHeight:20];
    [self.tempLab mas_updateConstraints:^(MASConstraintMaker *make) {
        make.width.offset(tempWidth);
    }];
    
    [self.volLab mas_updateConstraints:^(MASConstraintMaker *make) {
        make.width.offset(volWidth);
    }];
}

-(void)hideBottomLine:(BOOL)hidden
{
    self.lineLayer.frame = CGRectMake(0, 0, self.width, hidden?self.height-20:self.height);
}

- (void)setupUI{
    kWeakSelf;
    [self setBackgroundColor:UIColor.clearColor];
    [self.contentView setBackgroundColor:CFTabViewBgColor];
    
    CAShapeLayer *layer = [CAShapeLayer layer];
    layer.frame = self.contentView.bounds;
    layer.strokeColor = kCOLOR_HEX(@"CCCCCC").CGColor;
    layer.lineWidth = 1;
    layer.lineCap = kCALineCapSquare;
    layer.lineDashPattern = @[@(2),@(2)];
    UIBezierPath *path = [UIBezierPath bezierPath];
    [path moveToPoint:CGPointMake(self.width/2, 0)];
    [path addLineToPoint:CGPointMake(self.width/2, self.height)];
    layer.masksToBounds = YES;
    layer.path = path.CGPath;
    [self.contentView.layer addSublayer:layer];
    self.lineLayer = layer;
    
    UIView *bgView = [[UIView alloc] init];
    bgView.layer.cornerRadius = 5;
    bgView.layer.masksToBounds = YES;
    [bgView setBackgroundColor:UIColor.whiteColor];
    [self.contentView addSubview:bgView];
    
    [bgView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.insets(UIEdgeInsetsMake(10,0, 10, 0));
    }];
    
    _batIcon = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"local_cell_temp_normal"]];
    [bgView addSubview:_batIcon];
    [_batIcon mas_makeConstraints:^(MASConstraintMaker *make) {
        make.width.height.offset(18);
        make.left.mas_offset(10);
        make.top.mas_offset(10);
    }];
    
    _titleLab = [[QMUILabel alloc]init];
    _titleLab.textAlignment = NSTextAlignmentLeft;
    _titleLab.text = @"1#电芯";
    _titleLab.textColor = [UIColor colorWithHexString:@"000000" alpha:0.8];
    _titleLab.font = [UIFont systemFontOfSize:11];
    [bgView addSubview:_titleLab];
    [_titleLab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(weakSelf.batIcon.mas_right).offset(5);
        make.centerY.equalTo(weakSelf.batIcon);
        
    }];
    
    UIView *line = kView;
    line.backgroundColor = kRGB(225, 225, 225);
    [bgView addSubview:line];
    [line mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.mas_offset(0);
        make.top.equalTo(weakSelf.titleLab.mas_bottom).offset(10);
        make.height.offset(1);
    }];
    
    
    
    
    QMUILabel *tempTitleLab = [QMUILabel labelWithText:[NSString stringWithFormat:@"%@:",@"cell_temperature".rh_localized] font:kSYSTEMFONT(10) textColor:kCOLOR_HEX(@"#3F4443")];
    tempTitleLab.numberOfLines = 2;
    tempTitleLab.adjustsFontSizeToFitWidth = YES;
    tempTitleLab.minimumScaleFactor = 0.8;
    tempTitleLab.textAlignment = NSTextAlignmentLeft;
    [bgView addSubview:tempTitleLab];
    [tempTitleLab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_offset(10);
        make.top.equalTo(line.mas_bottom).offset(15);
    }];
    
    _tempLab = [QMUILabel labelWithFont:kSYSTEMFONT(10) textColor:kCOLOR_HEX(@"#3F4443") textAligment:NSTextAlignmentRight];
    _tempLab.text = @"--";
    [bgView addSubview:_tempLab];
    [_tempLab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(tempTitleLab);
        make.right.mas_offset(-10);
        make.left.equalTo(tempTitleLab.mas_right);
    }];
    
    QMUILabel *volTitleLab = [QMUILabel labelWithText:[NSString stringWithFormat:@"%@:",@"电压".rh_localized] font:kSYSTEMFONT(10) textColor:kCOLOR_HEX(@"#3F4443")];
    volTitleLab.textAlignment = NSTextAlignmentLeft;
    volTitleLab.numberOfLines = 2;
    volTitleLab.adjustsFontSizeToFitWidth = YES;
    volTitleLab.minimumScaleFactor = 0.8;
    [bgView addSubview:volTitleLab];
    [volTitleLab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(tempTitleLab);
        make.bottom.mas_offset(-15);
    }];
    
    _volLab = [QMUILabel labelWithFont:kSYSTEMFONT(10) textColor:kCOLOR_HEX(@"#3F4443") textAligment:NSTextAlignmentRight];
    _volLab.text = @"--";
    [bgView addSubview:_volLab];
    [_volLab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(volTitleLab);
        make.right.mas_offset(-10);
        make.left.equalTo(volTitleLab.mas_right);
    }];
    

    
}



@end
